/*-------------------------------------------------
   工作流试图核心  组件
    开发人：tss
    开发时间：2010-08-23
    修改人： 
---------------------------------------------------*/

//定义初始化变量
var DEFAULT_WIDTH = 90;
var DEFAULT_HEIGHT = 35;
var DEFAULT_POSLEFT = 10;
var DEFAULT_POSTOP = 10;
var DEFAULT_CATEGORY = "common";

/**
 * 定义节点类
 */
function WorkFlowNode(jsonObject) {
	
	this.jsonObject = jsonObject;
	this.id = jsonObject.form.id;
	this.name = jsonObject.form.name;

}

/**
 * 画节点
 * @param x
 * @param y
 * @param w
 * @param h
 * @return
 */ 
WorkFlowNode.prototype.draw = function(x, y, w, h) {
	
	this.posLeft = x;
	this.posTop = y;
	this.posWidth = w;
	this.posHeight = h;
	var style = new Object();

	style[mxConstants.STYLE_SHAPE] = this.jsonObject.cssStyle.styleShape;
	var image = this.jsonObject.cssStyle.image;
	if(image != null && image != ""){
		style[mxConstants.STYLE_IMAGE] = image;
	}
	style[mxConstants.STYLE_STROKECOLOR] = '#000000';
	style[mxConstants.STYLE_FONTCOLOR] = this.jsonObject.cssStyle.foregroundColor;
	style[mxConstants.STYLE_GRADIENTCOLOR] = this.jsonObject.cssStyle.backgroundColor;
	style[mxConstants.STYLE_ROUNDED] = true;
	style[mxConstants.STYLE_SHADOW] = true;
	graph.getStylesheet().putCellStyle('boxstyle', style);

   
	this.cell = graph.insertVertex(graph.parent, this.id, this.name,
			this.posLeft, this.posTop, this.posWidth, this.posHeight,
			'boxstyle');
	
	
	//this.cell.object = this;
	this.cell.form = new Object();
	this.cell.cssStyle = this.jsonObject.cssStyle;
	this.cell.form.variableMap = new Map();
};

/**
 * 同步json表单数据到流程图对象
 * @return
 */
WorkFlowNode.prototype.syna = function() {

	with (this.cell) {
		for ( var prop in this.jsonObject.form) {
			if(prop == "variableMap"){
				var dataList = this.jsonObject.form[prop].dataList;
				if(dataList != null && dataList.length > 0){
					for(var i = 0; i < dataList.length; i++){
						var data = dataList[i];
						form[prop].put(data.key,data.value);
					}
				}
			}else{
				form[prop] = this.jsonObject.form[prop];
			}
		}
		if (form.id == "") { // 如果节点id为空，则设置一个
			form.id = value + id;
		}
	}
};

/**
 * 定义链接
 * @param jsonObject
 * @return
 */
function WorkFlowLink(jsonObject) {
	this.jsonObject = jsonObject;
	if (typeof (jsonObject.form) == "undefined") {
		jsonObject.form = new Object();
	}
	this.id = jsonObject.form.id;
	this.name = jsonObject.form.name;
}

/**
 * 画链接
 * @param inputCell
 * @param outputCell
 * @return
 */
WorkFlowLink.prototype.draw = function(inputCell,outputCell,_cell) {

	if(_cell != null){
		this.cell = _cell;
	}else{
		
		
		var style = new Object();
		graph.getStylesheet().putCellStyle('linkStyle', style);
		style[mxConstants.STYLE_ROUNDED] = true;
		style[mxConstants.STYLE_EDGE] =  mxEdgeStyle.TopToBottom;
		
		this.cell = graph.insertEdge(graph.parent, this.jsonObject.form.id,
				this.jsonObject.form.name, inputCell, outputCell, 'linkStyle');
	}
	if(this.cell.geometry.points == null){
		this.cell.geometry.points = new Array();
	}
	
	var points = this.jsonObject.cssStyle.points;
	if(points){
		for(var i = 0; i < points.length; i++){
			this.cell.geometry.points.push(points[i]);
		}
	}
	//this.cell.object = this;
	this.cell.form = new Object();
	this.cell.cssStyle = new Object();
	this.cell.form.variableMap = new Map();
};

/**
 * 同步json表单数据到流程图对象
 * @return
 */
WorkFlowLink.prototype.syna = function() {
	with (this.cell) {
		for ( var prop in this.jsonObject.form) {
			form[prop] = this.jsonObject.form[prop];
		}
		for(var prop in this.jsonObject.cssStyle){
			cssStyle[prop] = this.jsonObject.cssStyle[prop];
		}
	}
};

